﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HigLabo.Net.Extensions;

namespace HigLabo.Net.WindowsLive
{
    /// <summary>
    /// 
    /// </summary>
    public class LiveObjectCollection : Collection<LiveObject>
    {
        /// <summary>
        /// 
        /// </summary>
        public List<File> Files
        {
            get
            {
                return this.Where(a => a.Type == FileType.file.ToString()).Select(a => (File)a).ToList();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<Album> Albums
        {
            get
            {
                return this.Where(a => a.Type == FileType.album.ToString()).Select(a => (Album)a).ToList();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<Audio> Audios
        {
            get
            {
                return this.Where(a => a.Type == FileType.audio.ToString()).Select(a => (Audio)a).ToList();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<Folder> Folders
        {
            get
            {
                return this.Where(a => a.Type == FileType.folder.ToString()).Select(a => (Folder)a).ToList();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<Photo> Photos
        {
            get
            {
                return this.Where(a => a.Type == FileType.photo.ToString()).Select(a => (Photo)a).ToList();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public List<Video> Videos
        {
            get
            {
                return this.Where(a => a.Type == FileType.video.ToString()).Select(a => (Video)a).ToList();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public LiveObjectCollection(String jsonText)
        {
            this.SetProperty(jsonText);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsonText"></param>
        public void SetProperty(String jsonText)
        {
            var d = Common.SetData(jsonText).ToString("data");
            var cc = JsonConvert.DeserializeObject(d) as JContainer;

            foreach (var s in WindowsLiveClient.CreateJsonTextList(cc))
            {
                LiveObject tmp;
                switch ((FileType)Enum.Parse(typeof(FileType), Common.SetData(s).ToString("type"), true))
                {
                    case FileType.folder:
                        tmp = new Folder(s);
                        break;
                    case FileType.album:
                        tmp = new Album(s);
                        break;
                    case FileType.audio:
                        tmp = new Audio(s);
                        break;
                    case FileType.file:
                        tmp = new File(s);
                        break;
                    case FileType.photo:
                        tmp = new Photo(s);
                        break;
                    case FileType.video:
                        tmp = new Video(s);
                        break;
                    default:
                        continue;
                }
                this.Add(tmp);
            }
        }
    }

}
